package com.fm.financemanage.controller.sys;



import com.fm.financemanage.component.TokenComponent;
import com.fm.financemanage.model.po.SysDictData;
import com.fm.financemanage.model.po.SysUser;
import com.fm.financemanage.response.Callback;
import com.fm.financemanage.response.JsonData;
import com.fm.financemanage.service.SysDictDataService;
import com.fm.financemanage.utils.TextUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.Min;
import java.util.HashMap;
import java.util.List;

/**
 * @author : zpc
 * @version V1.0
 * @date :
 */
@Api(tags = "【web端】数据字典")
@RestController
@RequestMapping("/sys/dict/data")
public class SysDictDataController {

    @Autowired
    private SysDictDataService dictDataService;

    @Autowired
    private TokenComponent tokenComponent;

    @RequiresPermissions(value = {"sys:dict:data:get"})
    @ApiOperation("根据关键词查询数据")
    @GetMapping("/")
    public JsonData list(@RequestParam(name = "dictType", required = false) String dictType,
                         @RequestParam(name = "dictLabel", required = false) String dictLabel,
                         @RequestParam(name = "state", required = false) Integer state,
                         @RequestParam(name = "page", defaultValue = "1", required = false) @Min(1) Integer page,
                         @RequestParam(name = "limit", defaultValue = "10", required = false) @Min(1) Integer limit) {

        List<SysDictData> list = dictDataService.selectDictDataList(dictType, dictLabel, state, (page - 1) * limit, limit);
        int count = dictDataService.countDataList(dictType, dictLabel, state);
        HashMap<Object, Object> map = new HashMap<>();
        map.put("list", list);
        map.put("count", count);
        return Callback.success("200", "成功", map);

    }


    /**
     * 新增保存字典类型
     */
    @RequiresPermissions(value = {"sys:dict:data:post"})
    @ApiOperation("新增保存字典类型")
    @PostMapping("/")
    public JsonData addSave(HttpServletRequest servletRequest, @RequestBody SysDictData dict) {

        String authorization = servletRequest.getHeader("Authorization");
        SysUser byToken = tokenComponent.getByToken(authorization);
        if (byToken == null || TextUtil.isEmpty(byToken.getId())) {
            return Callback.fail("404", "暂无用户信息");
        }
        dict.setCreateBy(byToken.getLoginName());
        dictDataService.insertDictData(dict);
        return Callback.success("200", "成功");

    }

    /**
     * 修改保存字典类型
     */
    @RequiresPermissions(value = {"sys:dict:data:put"})
    @ApiOperation("修改保存字典类型")
    @PutMapping("/")
    public JsonData editSave(HttpServletRequest servletRequest, @RequestBody SysDictData dict) {

        String authorization = servletRequest.getHeader("Authorization");
        SysUser byToken = tokenComponent.getByToken(authorization);
        if (byToken == null || TextUtil.isEmpty(byToken.getId())) {
            return Callback.fail("404", "暂无用户信息");
        }
        dict.setCreateBy(byToken.getLoginName());
        dictDataService.updateDictData(dict);
        return Callback.success("200", "成功");


    }

    @RequiresPermissions(value = {"sys:dict:data:delete"})
    @ApiOperation("删除保存字典类型")
    @DeleteMapping("/")
    public JsonData remove(
            @ApiParam(name = "ids", value = "主键集合")
            @RequestParam(name = "ids", required = true) String ids) {

        if (TextUtil.isEmpty(ids)) {
            return Callback.fail("404", "参数不允许为空");
        }
        dictDataService.deleteDictDataByIds(ids);
        return Callback.success("200", "成功");


    }
}
